package mygame.myandroidgameframework;

import mygame.basicgameframework.Audio;
import mygame.basicgameframework.FileIO;
import mygame.basicgameframework.Game;
import mygame.basicgameframework.Graphics;
import mygame.basicgameframework.Input;
import mygame.basicgameframework.Screen;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;

public class MyAndroidGame extends Activity implements Game {
	
	MyAndroidFastRenderView renderView; 
    Graphics graphics; 
    Audio audio; 
    Input input; 
    FileIO fileIO; 
    Screen screen; 
    WakeLock wakeLock;
    
    // called when the activity is started
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    			WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	
    	// check if landscape or portrait
    	boolean isLandscape = getResources().getConfiguration().orientation == 
    		Configuration.ORIENTATION_LANDSCAPE ; 
    	int frameBufferWidth = isLandscape ? 480 : 320; 
        int frameBufferHeight = isLandscape ? 320 : 480; 
        Bitmap frameBuffer = Bitmap.createBitmap (frameBufferWidth, 
        		frameBufferHeight, Config.RGB_565 ); 
        float scaleX = (float) frameBufferWidth 
    		                / getWindowManager().getDefaultDisplay().getWidth(); 
        float scaleY = (float) frameBufferHeight 
    		                / getWindowManager().getDefaultDisplay().getHeight(); 

        renderView = new MyAndroidFastRenderView(this, frameBuffer); 
        graphics = new MyAndroidGraphics(getAssets(), frameBuffer); 
        fileIO = new MyAndroidFileIO(getAssets()); 
        audio = new MyAndroidAudio(this); 
        input = new MyAndroidInput(this, renderView, scaleX, scaleY); 
        screen = getStartScreen(); 
        setContentView(renderView); 
        
        PowerManager powerManager = (PowerManager) 
        getSystemService(Context.POWER_SERVICE ); 
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK , "GLGame");
    }
    
    @Override 
    public void onResume() { 
        super.onResume(); 
        wakeLock.acquire(); 
        screen.resume(); 
        renderView.resume(); 
    }

    @Override 
    public void onPause() { 
        super.onPause(); 
        wakeLock.release(); 
        renderView.pause(); 
        screen.pause(); 
 
        if (isFinishing()) 
            screen.dispose(); 
    }
    
	public Input getInput() {
		return input;
	}

	public FileIO getFileIO() {
		return fileIO;
	}

	public Graphics getGraphics() {
		return graphics;
	}

	public Audio getAudio() {
		return audio;
	}

	public void setScreen(Screen screen) {
		if (screen == null) 
            throw new IllegalArgumentException("Screen must not be null"); 
 
        this.screen.pause(); 
        this.screen.dispose(); 
        screen.resume(); 
        screen.update(0); 
        this.screen = screen;
	}

	public Screen getCurrentScreen() {
		return screen;
	}

	public Screen getStartScreen() {
		// do nothing
		return null;
	}

}
